package cn.dengta.webapp.trade.dto;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.dengta.webapp.trade.config.IntegralTradeProperties;
import cn.dengta.webapp.trade.model.Currency;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Assert;
import me.codeplayer.util.CollectionUtil;
import org.springframework.beans.factory.InitializingBean;

/**
 * 人民币兑其他货币的汇率配置
 */
@Getter
@Setter
public class ExchangeRateConfig implements InitializingBean {

	/** 其他货币 兑 人民币 的汇率，即 1元人民币 可以兑换 多少单位 的其他货币 */
	protected Map<String, BigDecimal> currency2Rmb;

	public ExchangeRateConfig initDefault() {
		currency2Rmb = CollectionUtil.toMap(LinkedHashMap::new,
				Currency.RMB.name(), BigDecimal.ONE,
				Currency.USD.name(), BigDecimal.valueOf(0.1431),
				Currency.VND.name(), BigDecimal.valueOf(3379.8628),
				Currency.TWD.name(), BigDecimal.valueOf(4.4013),
				Currency.KRW.name(), BigDecimal.valueOf(183.3196),
				Currency.JPY.name(), BigDecimal.valueOf(18.9767),
				Currency.INR.name(), BigDecimal.valueOf(11.8526),
				Currency.MYR.name(), BigDecimal.valueOf(0.6334),
				Currency.THB.name(), BigDecimal.valueOf(4.9678),
				Currency.IDR.name(), BigDecimal.valueOf(2219.7404),
				Currency.PHP.name(), BigDecimal.valueOf(7.9015),
				Currency.MAD.name(), BigDecimal.valueOf(4.4245),
				Currency.TRY.name(), BigDecimal.valueOf(4.4245),
				Currency.AED.name(), BigDecimal.valueOf(0.5077),
				Currency.SAR.name(), BigDecimal.valueOf(0.5186),
				Currency.CAD.name(), BigDecimal.valueOf(0.188),
				Currency.GBP.name(), BigDecimal.valueOf(0.1094),
				Currency.EUR.name(), BigDecimal.valueOf(0.1276),
				Currency.BRL.name(), BigDecimal.valueOf(0.7004),
				Currency.MXN.name(), BigDecimal.valueOf(2.2776),
				Currency.PEN.name(), BigDecimal.valueOf(0.5089),
				Currency.ARS.name(), BigDecimal.valueOf(119.2657)
		);
		return this;
	}

	public BigDecimal getExchangeRate(Currency currency) {
		return currency2Rmb.get(currency.name());
	}

	/**
	 * 计算将 积分 兑换为 指定货币（结果保留两位小数）
	 */
	public BigDecimal calcIntegralToCurrency(BigDecimal integral, Currency currency) {
		final IntegralTradeProperties cfg = IntegralTradeProperties.get();
		BigDecimal rmb = cfg.calcIntegralToRMB(integral, cfg.getIntegralToRMBDivisor());
		return currency == Currency.RMB ? rmb : RMBToCurrency(rmb, currency);
	}

	/**
	 * 计算将 人民币 兑换为 指定货币（结果保留 2 位小数）
	 */
	public BigDecimal RMBToCurrency(BigDecimal rmb, Currency currency) {
		return RMBToCurrency(rmb, currency, RoundingMode.FLOOR);
	}

	/**
	 * 计算将 人民币 兑换为 指定货币（结果保留 2 位小数）
	 */
	public BigDecimal RMBToCurrency(BigDecimal rmb, Currency currency, RoundingMode roundingMode) {
		BigDecimal rate = getExchangeRate(currency);
		return rmb.multiply(rate).setScale(2, roundingMode);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(currency2Rmb, "缺失汇率参数配置！");
		for (Currency currency : Currency.CACHE) {
			final BigDecimal decimal = currency2Rmb.get(currency.name());
			if (decimal == null) {
				throw new IllegalArgumentException("缺少 " + currency.name() + "兑人民币 的汇率配置！");
			}
		}
	}

}
